<?php
/**
 * MyaccountController.php
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to version 2.0 of the Apache license
 * that is available through the world-wide-web at the following URI:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * @category Controller
 * @package  Controller
 * @author   Alex Kopp <koppa@rpi.edu>
 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link     http://http://code.google.com/p/tri-chi/
 */

/**
 * MyaccountController
 *
 * The controller that allows users to manage their account
 *
 * @category Controller
 * @package  Controller
 * @author   Alex Kopp <koppa@rpi.edu>
 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link     http://http://code.google.com/p/tri-chi/
 */
class MyaccountController extends Controller
{
    public $layout='//layouts/main';
    
    /**
     * This is the list of filters we would like to enable for this controller.
     * 
     * We want to limit access to this controller... Thus, including 'accessControl'
     * 
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl',
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * 
     * Only administrators should be permitted to manage other Users
     * 
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow',
                'actions'=>array('index'),
                'roles'=>array('member'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

    /**
     * Presents and processes form to change user password
     * 
     * @return void
     */
    public function actionIndex()
    {
        $model=new ChangePasswordForm;
        if (isset($_POST['ChangePasswordForm'])) {
            $model->attributes=$_POST['ChangePasswordForm'];
            if ($model->validate()) {
                $memberModel=Users::model()->findByPk(Yii::app()->user->name);
                $memberModel->password = sha1($model->newPassword);
                $memberModel->save();
                Yii::app()->user->setFlash(
                    'changepassword', 'Successfully Changed Password'
                );
                $this->refresh();
            }
        }
        $this->render('index', array('model'=>$model));

    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * 
     * @param integer $id the username to be loaded
     * 
     * @return User
     */
    public function loadModel($id)
    {
        $model=Users::model()->findByPk($id);
        if ($model===null) {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * 
     * @param CModel $model the model to be validated
     * 
     * @return void
     */
    protected function performAjaxValidation($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax']==='users-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
